package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.OrderBean;
import com.project.bean.SupplyBean;
import com.project.bean.SupplyGoodsBean;
import com.project.mapper.IOrderMapper;
import com.project.mapper.ISupplyGoodsMapper;
import com.project.mapper.ISupplyMapper;
import com.project.service.IOrderService;
import com.project.util.ConvertBeanUtil;
import com.project.vo.SupplyVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.List;

@Service
@Transactional
public class OrderServiceImpl implements IOrderService {
    @Autowired
    private IOrderMapper orderMapper;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ISupplyMapper supplyMapper;
    @Autowired
    private ISupplyGoodsMapper supplyGoodsMapper;
    @Override
    public boolean add(Long supplierId, String supplierName, String pact) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("o_pact", pact);
        Long count = orderMapper.selectCount(qw);
        if (count != 0) {
            return false;
        }

        OrderBean order = new OrderBean();
        order.setPact(pact);
        order.setSupplierId(supplierId);
        order.setSupplierName(supplierName);
        String orderId = "" + supplierId + System.currentTimeMillis();
        order.setOrderId(orderId);
        QueryWrapper<SupplyGoodsBean> qw1 = new QueryWrapper<>();
        qw1.eq("sg_pact", pact);
        List<SupplyGoodsBean> list = supplyGoodsMapper.selectList(qw1);
        double sumPrice=0.0;
        for (int i = 0; i < list.size(); i++) {
            SupplyGoodsBean goodsBean = list.get(i);
            sumPrice+=goodsBean.getSum()* goodsBean.getPrice();
        }
        order.setSumPrice(sumPrice);
        orderMapper.insert(order);

        //发送合同编号给审核
        rabbitTemplate.convertAndSend("supplierOne","supplier.sendOrder",pact);

        return true;
    }

    @Override
    public IPage<OrderBean> cutByItem(Long supplierId, String pact, LocalDate startTime,LocalDate endTime,Integer pageNO) {
        QueryWrapper<OrderBean> qw = new QueryWrapper<>();
        qw.eq("fk_supplierId",supplierId);
        qw.orderByDesc("o_time");
        if (pact !=null && pact.length()!=0){
            qw.like("o_pact",pact);
        }
        if (endTime != null) {
            qw.le("o_time", endTime);
        }
        if (startTime != null) {
            qw.ge("o_time", startTime);
        }
        return orderMapper.selectPage(new Page<>(pageNO,3),qw);
    }

    @Override
    public OrderBean findById(Long id) {
        OrderBean orderBean = orderMapper.selectById(id);
        QueryWrapper<SupplyGoodsBean> qw = new QueryWrapper<>();
        qw.eq("sg_pact", orderBean.getPact());
        List<SupplyGoodsBean> supplyGoodsList = supplyGoodsMapper.selectList(qw);
        orderBean.setSupplyGoodsList(supplyGoodsList);
        return orderBean;
    }

    @Override
    public List<SupplyVo> findSupplyList(String orderId) {
        List<SupplyBean> supplyList = supplyMapper.findSupplyList(orderId);
        List<SupplyVo> supplyVoList = ConvertBeanUtil.copyList(supplyList, SupplyVo.class, (m, n) -> {
            n.setStatus("");
        });

        return supplyVoList;
    }
}
